<script>
	import region from "@/common/data.js";
	var globalEvent = uni.requireNativePlugin('globalEvent');
	var WXSDKInstanceModule;
	import {
		mapState,
		mapMutations,
		updatePushMessage,
		updateCodeResults
	} from 'vuex'
	// var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
	const huaWeiScan = uni.requireNativePlugin('huaWei-scan');
	// var mpaasScanModule = ''
	export default {
		data() {
			return {
				timeVideo: null,
				dayTime: null,
				watchTheTime: 0,
				backButtonPress: 0, //2次退出应用计时
				userInfo: "",
				count: 0, //控制递归次数
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef APP-PLUS
			//禁止横屏
			globalEvent.addEventListener('onResp', res=> {
				if(res.result.scene){
					this.wxPushSubscribe(res.result);
				}
			});
			
			const _self = this;  
			const _handlePush = function(message) {  
				/**  
				 * 通过 vuex 来同步页面的数据，仅做演示。  
				 * 实际开发中，这里可能是跳转到某个页面等操作，请根据自身业务需求编写。  
				 */  
				_self.updatePushMessage(message);  
			};  
			globalEvent.addEventListener('onActivityChangeResult', res=> {
				_self.updateCodeResults(res);  
			});
			plus.push.addEventListener('click', function(message) {  
				plus.nativeUI.toast('push click');  
				_handlePush(message);  
			});  
			plus.push.addEventListener('receive', function(message) {  
				plus.nativeUI.toast('push receive');  
				_handlePush(message);  
			});  
			plus.screen.lockOrientation("portrait-primary");
			
			if(plus.navigator.isSimulator()){
				uni.showToast({
					title:"禁止模拟器安装此软件",
					icon: 'none',
					duration: 2000,
				})
				setTimeout(()=>{
					plus.runtime.quit();
				},3000)
			}
			this.getAD();
			this.$util.doPostJson('/report/openAppNum', {}, {}, false).then(res => {
				if (res.data.code == this.$ResponseStatus.OK) {
					console.log("app启动次数记录+1", res.data)
				}
			}).catch(error => {
				uni.hideLoading()
				console.log(error)
			})
			switch (uni.getSystemInfoSync().platform) {
				case 'android':
					//console.log('运行Android上') 
					WXSDKInstanceModule = uni.requireNativePlugin('WXSDKInstanceModule');
					WXSDKInstanceModule.creation();
					setTimeout(() => {
						plus.navigator.closeSplashscreen();
					}, 500)
					break;
				case 'ios':
					// console.log('运行iOS上') 
					this.globalData.isAndroid = false
					setTimeout(() => {
						plus.navigator.closeSplashscreen();
					}, 500)

					break;
				default:
					//console.log('运行在开发者工具上') 
					break;
			}
			// #endif
		},
		computed: {
			...mapState(['hasLogin', 'watchTime']),
		},
		watch: {
			hasLogin(hasLogin) {
				// Our fancy notification (2).
				console.log("在线时长统计开关", hasLogin)
				if (hasLogin) {
					this.setUserTime();
				} else {
					this.stopUserTime();
				}
			},
			watchTime(watchTime) {
				console.log("当前在线总时长", watchTime)
				this.watchTheTime = watchTime;
				this.setUserTime();
			}
		},
		onShow: function() {
			console.log('App Show')
			// #ifdef APP-PLUS
			this.login();
			// #endif
			
		},
		onHide: function() {
			console.log('App Hide')
			// #ifdef APP-PLUS
			this.logout();
			// #endif
		},
		methods: {
			...mapMutations(['logout']),
			...mapMutations(['login']),
			...mapMutations(['updatePushMessage']),
			...mapMutations(['updateCodeResults']),
			getWXSDKInstanceModule(){
				return WXSDKInstanceModule
			},
			wxPushSubscribe(data){
				console.log("微信一次性消息授权回调",data)
				this.$util.doPostJson('/wx/apiPushSubscribe', data, {}, true).then(res => {
					if (res.data.code == this.$ResponseStatus.OK) {
						
					}
				}).catch(error => {
					uni.hideLoading()
					console.log(error)
				})
			},
			/* 加载广告位 */
			getAD(){
				this.$util.doGet('/adpInfo/getRandomAdpId', {}, {}, false).then(res => {
					if (res.data.code == this.$ResponseStatus.OK) {
						uni.setStorageSync(this.$util.ADPID,res.data.data)
					}else {
						uni.setStorageSync(this.$util.ADPID,"1867827506")
					}
				}).catch(error => {
					uni.hideLoading()
					console.log(error)
				})
			},
			shipmentSocket(id,userInfo,bagProduct) {
				uni.connectSocket({
					url: this.$util.SOCKET_URL + `/webSocket/yunBag/${id}`,
				});
				uni.onSocketOpen(res1 => {
					this.$util.showInfoToast("正在出货中，请耐心等待")
				});
				uni.onSocketMessage(res2 => {
					console.log(res2.data)
					let data = JSON.parse(res2.data)
					if (data.code == this.$ResponseStatus.OK) {
						userInfo.totalPoints -= bagProduct.itemValue
						this.open("出货成功",'您兑换的环保袋已经成功出货')
					} else {
						this.open("出货失败",'兑换失败，失败不会扣除您的积分')
					}
					uni.closeSocket();
				});
			},
			open(message,messageContent) {
				let params = {
					inApp: true, // app内横幅提醒
					voice: true,    // 声音提醒
					vibration: true,    // 振动提醒
					messageType: '系统消息',
					messageTitle: message,
					messageContent: messageContent,
					messageImage: '/static/icon/LOGO.png'
				}
				this.$appPush(params)
			},
			async setUserTime() {
				if (this.timeVideo) {
					return
				}
				if (uni.getStorageSync(this.$util.HAS_USER_INFO)) {
					this.userInfo = uni.getStorageSync(this.$util.USER_INFO) || ''
					if (this.userInfo) {
						this.count = 0;
						this.dayTime = "user_" + this.userInfo.rcode + '_' + this.$util.getTime().split(" ")[0]
						this.timeVideo = setInterval(() => {
							this.watchTheTime++
							uni.setStorageSync(this.dayTime, this.watchTheTime)
						}, 1000)
					}
				}
			},
			stopUserTime() {
				if (this.timeVideo) {
					clearInterval(this.timeVideo);
					this.timeVideo = null;
					if (uni.getStorageSync(this.$util.HAS_USER_INFO)) {
						console.log("当前在线时长", this.watchTheTime)
						this.$util.updateUserTime(this.watchTheTime)
					}
				}
			},
			getScan(){
				console.log("插件",huaWeiScan)
				return huaWeiScan
			},
			// getMpaasScanModule() {
			// 	return mpaasScanModule
			// }
		},
		globalData: {
			types: "TB",
			isAndroid:true
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
